﻿@model Tuple<List<deviceManage.Models.DeviceType>, deviceManage.Models.ServiceContract>
@{
    ViewData["Title"] = "新增运维合同";
}

<style>
    .layui-form-label{
        width:90px;
    }
    .layui-layout-body {
        overflow: auto;
    }
</style>
 
        <div class="layui-fluid" style="padding-bottom:85px;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md8">
                    <form class="layui-form" action="doCreate" lay-filter="saveForm">
                        <input type="hidden" name="id" id="id" value="@Model.Item2.Id" />
                        <div class="layui-card">
                            <div class="layui-card-header">设备类型</div>
                            <div class="layui-card-body" style="padding: 15px;">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">设备类型</label>
                                        <div class="layui-input-inline">
                                            <select name="deviceTypeId">
                                                @foreach (var x in Model.Item1)
                                                {
                                                    @if (x.Id.Equals(Model.Item2.DeviceTypeId))
                                                    {
                                                        <option selected value="@x.Id">@x.Name</option>
                                                    }
                                                    else
                                                    {
                                                        <option value="@x.Id">@x.Name</option>
                                                    }

                                                }
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-card">
                            <div class="layui-card-header">合同内容</div>
                            <div class="layui-card-body" style="padding: 15px;">

                                <div class="layui-form-item">
                                    <div class="layui-block">
                                        <label class="layui-form-label">运维主题</label>
                                        <div class="layui-input-inline" style="width:521px;">
                                            <input type="text" name="title" value="@Model.Item2.Title" lay-verify="required" autocomplete="off" class="layui-input" maxlength="100">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">合同时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly="readonly" value="@if(Model.Item2.StartDate != null){@Model.Item2.StartDate.ToString().Substring(0, 10);} " name="StartDate" id="StartDate" lay-verify="required" autocomplete="off" class="layui-input date">

                                        </div>

                                    </div>

                                    <div class="layui-inline">
                                        <label class="layui-form-label">到期时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly="readonly" value="@if(Model.Item2.EndDate != null){@Model.Item2.EndDate.ToString().Substring(0, 10);}" name="EndDate" id="EndDate" lay-verify="required" autocomplete="off" class="layui-input date">
                                        </div>
                                    </div>

                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">甲方</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="Boss" value="@Model.Item2.Boss" lay-verify="required" autocomplete="off" class="layui-input" maxlength="80">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">乙方</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="Employee" value="@Model.Item2.Employee" lay-verify="required" autocomplete="off" class="layui-input" maxlength="80">
                                        </div>
                                    </div>

                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">合同金额(元）</label>
                                        <div class="layui-input-inline">
                                            <input style="text-align:right;padding-right:5px;" type="text" name="amount" value="@Model.Item2.Amount" lay-verify="money" autocomplete="off" class="layui-input" maxlength="20">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                </div>

                            </div>
                        </div>

                        <div class="layui-card">
                            <div class="layui-card-header">支付限制条件</div>
                            <div class="layui-card-body" style="padding: 15px;">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="width:130px;">维护完成进度（%）</label>
                                        <div class="layui-input-inline">
                                            <input type="number" value="@Model.Item2.DonePercent" name="DonePercent" lay-verify="number" autocomplete="off" class="layui-input" maxlength="3">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="width:129px;">维护完成次数</label>
                                        <div class="layui-input-inline">
                                            <input type="number" value="@Model.Item2.ServiceCountLimit" name="ServiceCountLimit" lay-verify="number" autocomplete="off" class="layui-input" maxlength="5">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <div class="layui-footer">
                                    <button class="layui-btn" lay-submit="" lay-filter="save">保存</button>
                                    <button type="button" onclick="window.parent.layer.closeAll()" class="layui-btn layui-btn-primary">返回</button>
                                </div>
                            </div>
                        </div>

                    </form>
                </div>
                <div class="layui-col-md4">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            合同附件
                            <button type="button" class="layui-btn layui-btn-xs" id="uploadBtn" style="margin-left:20px;">
                                <i class="layui-icon layui-icon-upload-drag"></i>上传
                            </button>
                        </div>
                        <div class="layui-card-body" style="padding: 15px;">
                            <table class="layui-table layuiadmin-page-table" id="filesTable" lay-skin="line"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
  
<script>

 


    form.render(null, 'saveForm');

   
    var insStart = laydate.render({
        elem: '#StartDate'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });
    var insEnd = laydate.render({
        elem: '#EndDate'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });


    /* 自定义验证规则 */
    form.verify({
        money: [/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, '金额格式不正确']
    });



    //加载table
    var tableObj = table.render({
        elem: '#filesTable'
        ,height:'550px'
        , url: '/doc/getAllByObjectId?id=@Model.Item2.Id'
        , cols: [[
            { field: 'name', title: '文件名称',  templet: '<div><a class="fileLink" href="/doc/download?id={{d.id}}">{{ d.name}}</a></div>' }
            , {
                field: 'opr', title: '操作', width: 80,
                templet: '<div class="layui-table-cell laytable-cell-11-0-8"><a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\'{{ d.id }}\')"><i class="layui-icon layui-icon-delete"></i>删除</a> </div> '
            }
        ]]
        , skin: 'line'
    });


    /* 监听提交 */
    form.on('submit(save)', function (data) {
        //开始保存合同信息
        $.post("/ServiceContract/save", data.field,
            function (e) {
               layer.msg(e.msg);
                if (e.code == 1) { //保存成功
                    window.parent.tableObj.reload();
                    window.parent.layer.closeAll();
              }
            });
            return false;
    });


    //上传文件
    var id = $('#id').val();
    var uploadInst = upload.render({
        elem: '#uploadBtn' //绑定元素
        , url: '/doc/upload?category=1&key='+id //上传接口
        , accept:'file'
       , done: function (res) {

       layer.msg(res.msg);
            if (res.code == 1) {//上传成功，刷新附件table
                tableObj.reload();
            }
        }
        , error: function () {
        layer.msg("上传失败！");
       }
    });

    //删除附件
    function deleteItem(id) {
        window.parent.layer.confirm("确定要删除附件吗？", function (index){
            window.parent.layer.close(index);
            $.post("/doc/delete?id=" + id,
                function (res) {
                         layer.msg(res.msg);
                          if (res.code == 1) {
                                   tableObj.reload();
                      }
           });
        });
    }

</script>
